package com.james.customcamera.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.james.customcamera.CustomCameraApplication;
import com.james.customcamera.R;
import com.james.customcamera.adapter.LoadMoreWrapper;
import com.james.customcamera.adapter.LoadMoreWrapperAdapter;
import com.james.customcamera.base.BaseFragment;
import com.james.customcamera.bean.HomeBean;
import com.james.customcamera.bean.PhotoBean;
import com.james.customcamera.contract.HomeContract;
import com.james.customcamera.listener.EndlessRecyclerOnScrollListener;
import com.james.customcamera.listener.onRecyclerItemClickListener;
import com.james.customcamera.presenter.HomePresenter;
import com.james.customcamera.ui.activity.PhotoActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment<HomePresenter> implements HomeContract.HomeView {

    @BindView(R.id.home_recycler_view)
    RecyclerView homeRecyclerView;
    @BindView(R.id.home_swipe_refresh_layout)
    SwipeRefreshLayout homeSwipeRefreshLayout;
    Unbinder unbinder;
    private int page = 0;
    private LoadMoreWrapper loadMoreWrapper;
    private List<PhotoBean> dataList = new ArrayList<>();
    private boolean isLastPage = false;

    @Override
    protected void initView() {
        mPresenter.getPhotos(0);
        LoadMoreWrapperAdapter loadMoreWrapperAdapter = new LoadMoreWrapperAdapter(dataList);
        loadMoreWrapper = new LoadMoreWrapper(loadMoreWrapperAdapter);
        homeRecyclerView.setLayoutManager(new GridLayoutManager(CustomCameraApplication.getContext(), 2));
        homeRecyclerView.setAdapter(loadMoreWrapper);
        // 设置下拉刷新
        homeSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                // 刷新数据
                dataList.clear();

                mPresenter.getPhotos(0);

                loadMoreWrapper.notifyDataSetChanged();

                // 延时1s关闭下拉刷新
                homeSwipeRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (homeSwipeRefreshLayout != null && homeSwipeRefreshLayout.isRefreshing()) {
                            homeSwipeRefreshLayout.setRefreshing(false);
                        }
                    }
                }, 1000);
            }
        });

        // 设置上拉加载更多监听
        homeRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
            @Override
            public void onLoadMore() {
                page++;
                mPresenter.loadMorePhotos(page);
                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected HomePresenter loadPresenter() {
        return new HomePresenter();
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void otherViewClick(final View view) {
        homeRecyclerView.addOnItemTouchListener(new onRecyclerItemClickListener(homeRecyclerView) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder) {
                Intent intent = new Intent(CustomCameraApplication.getContext(), PhotoActivity.class);
                intent.putExtra("photo_data", (Serializable) dataList);
                intent.putExtra("photo_position", viewHolder.getPosition());
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(RecyclerView.ViewHolder viewHolder) {

            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public boolean isActive() {
        return false;
    }


    @Override
    public void loadPhotos(HomeBean result) {
        if (result.getTotalCount() <= result.getPage()) {
            isLastPage = true;
        }
        dataList = result.getDataList();
    }

    @Override
    public void loadMorePhotos(HomeBean result) {
        if (result.getTotalCount() <= result.getPage()) {
            isLastPage = true;
        }
        dataList.addAll(result.getDataList());
        if (isLastPage == false) {
            // 模拟获取网络数据，延时1s
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //getData();
                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_COMPLETE);
                        }
                    });
                }
            }, 1000);
        } else if (isLastPage == true) {
            // 显示加载到底的提示
            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_END);
        }
    }

    @Override
    public void refreshPhotos(List<PhotoBean> result) {

    }

    @Override
    public void loadNoMorePhotos(List<PhotoBean> result) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
